<?php
namespace Admin\Controller;
use Lib\Api\AgentApi;
use Lib\Api\AgentCommissionSetApi;
use Lib\Api\GameTypeApi;
use Lib\Api\GameTypeProfitApi;
use Lib\Api\OrganizationApi;
use Lib\Api\OrganizationCommissionSetApi;
use Think\Controller;
use Think\Model;

class GameTypeController extends Controller {

    private $modelName = 'GameType';

    public function index(){
        $this->display();
    }

    public function tolist() {
        $modelObj = M();
        $field = "a.*, b.contact, b.profit_rate";
        $list = $modelObj->table('fs_game_type a')
            ->join('left join fs_game_type_profit b on b.type = a.id')
            ->field($field)
            ->order('b.contact asc')
            ->select();

        $new_arr = array();
        for ($i=0; $i<count($list); $i++) {
            $flag = false;
            $id = $list[$i]['id'];
            // $rebate_ratio_layer = $list[$i]['rebate_ratio_layer'];
            for ($j=0; $j<count($new_arr); $j++) {
                if ($new_arr[$j]['id'] == $id) {
                    $flag = true;
                    $new_arr[$j]["profit"] = $new_arr[$j]["profit"] . ' / ' . '触碰点位<span style="font-size:18px;color:#f43900">'.$list[$i]['contact'].'</span>;收益:'.$list[$i]['profit_rate'] . '%';
                    break;
                } else {
                    $flag = false;
                }
            }

            $profit = "未设置";
            if ($list[$i]['contact']!='') {
                $profit = '触碰点位<span style="font-size:18px;color:#f43900">'.$list[$i]['contact'].'</span>;收益:'.$list[$i]['profit_rate'] . '%';
            }
            if ($flag == false) {
                $obj["id"] = $id;
                $obj["game_name"] = $list[$i]['game_name'];
                $obj["show_name"] = $list[$i]['show_name'];
                $obj["name_code"] = $list[$i]['name_code'];
                $obj["norms"] = $list[$i]['norms'];
                $obj["state"] = $list[$i]['state'];
                $obj["profit"] = $profit;
                array_push($new_arr, $obj);
            }
        }
        $date["list"] = $new_arr;
        $this->ajaxReturn($date);
    }

    public function select_search() {
        $keyword = $_POST['keyword'];
        $map['show_name']  = array('like', '%'.$keyword.'%');
        $map['game_name']  = array('like','%'.$keyword.'%');
        $map['name_code']  = array('like','%'.$keyword.'%');
        $map['_logic'] = 'or';
        $where['_complex'] = $map;

        $list = GameTypeApi::getListAll($where,"id, show_name");
        $rel['list'] = $list;
        $this->ajaxReturn($rel);
    }

    public function changeState() {
        $modelObj = M($this->modelName);
        $this->ajaxReturn($modelObj->save($modelObj->create($_POST, Model::MODEL_UPDATE)));
    }

    public function addoredit() {
        $modelObj = M($this->modelName);
        $data = $modelObj->where(array('id' => $_GET['id']))->find();

        $modelObj = M("GameTypeProfit");
        $data["list_profit"] = $modelObj->where(array('type' => $_GET['id']))->order('contact asc')->select();

        // var_dump($data["list_profit"]);
        $this->assign('data', $data);
        $this->display();
    }

    public function addoreditdata() {
        $profit_rate_list = $_POST['profit_rate_list'];
        if (count($profit_rate_list) == 0) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '请先配置触摸点位';
            $this->ajaxReturn($ajaxdata);
            exit;
        }

        //数据校验
        if (empty($_POST['game_name'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写游戏名称';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($_POST['show_name'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写显示名称';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($_POST['name_code'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写名称编码';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($_POST['norms'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写规格';
            $this->ajaxReturn($ajaxdata);
        }
        if (empty($_POST['value_length']) && $_POST['value_length'] < 0) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写数值精确位数';
            $this->ajaxReturn($ajaxdata);
        }

        $modelObj = M($this->modelName);
        $data = $modelObj->create($_POST);
        if ($_POST['id'] > 0) {
            $modelObj->save($data);
            //删除之前的关联关系
            GameTypeProfitApi::delete(array('type' => $_POST['id']));

            for ($i=0; $i<count($profit_rate_list); $i++) {
                $profit_rate_list[$i]['type'] = $_POST['id'];
                $profit_rate_list[$i]['sort'] = ($i+1);
            }
            GameTypeProfitApi::addAll($profit_rate_list);
            $ajaxdata['code'] = 0;
            $ajaxdata['msg'] = '修改成功';
            $this->ajaxReturn($ajaxdata);
        } else {
            //添加账号是否存在
            $map['_query'] = 'game_name='.$data['game_name'].'&show_name='.$data['show_name'].'&name_code='.$data['name_code'].'&_logic=or';
            $result = GameTypeApi::getTotal($map);
            if ($result == 0) {
                $data['sort'] = 9999;
                $data['state'] = 1;
                $id = $modelObj->add($data);

                for ($i=0; $i<count($profit_rate_list); $i++) {
                    $profit_rate_list[$i]['type'] = $id;
                    $profit_rate_list[$i]['sort'] = ($i+1);
                }
                GameTypeProfitApi::addAll($profit_rate_list);
				
				// 添加机构返拥金设置
                $organization_list = OrganizationApi::getListAll(array(), "id, level");
                if (!empty($organization_list)) {
                    $add_organizationCommissionSet_arr = array();
                    for ($i=0; $i<count($organization_list); $i++) {
                        $commission_ratio = 0;
                        if ($organization_list[$i]["level"] == 0) {
                            $commission_ratio = 100;
                        }
                        $organizationCommissionSet["organization_id"] = $organization_list[$i]["id"];
                        $organizationCommissionSet["game_name"] = $data['show_name'];
                        $organizationCommissionSet["game_type"] = $id;
                        $organizationCommissionSet["commission_ratio"] = $commission_ratio;
                        $organizationCommissionSet["take_effect_time"] = date("Y-m-d H:i:s");;
                        $organizationCommissionSet["datetime"] = date("Y-m-d H:i:s");
                        array_push($add_organizationCommissionSet_arr, $organizationCommissionSet);
                    }
                    OrganizationCommissionSetApi::addAll($add_organizationCommissionSet_arr);
                }

                // 添加经纪人返佣设置
                $agent_list = AgentApi::getListAll(array("audit_state"=>1),"","id"); //OrganizationApi::getListAll(array(), "id, level");
                if (!empty($agent_list)) {
                    $add_agentCommissionSet_arr = array();
                    for ($i=0; $i<count($agent_list); $i++) {
                        $agentCommissionSet["agent_id"] = $agent_list[$i]["id"];
                        $agentCommissionSet["game_name"] = $data['show_name'];
                        $agentCommissionSet["game_type"] = $id;
                        $agentCommissionSet["commission_ratio"] = 0;
                        $agentCommissionSet["take_effect_time"] = date("Y-m-d H:i:s");;
                        $agentCommissionSet["datetime"] = date("Y-m-d H:i:s");
                        array_push($add_agentCommissionSet_arr, $agentCommissionSet);
                    }
                    AgentCommissionSetApi::addAll($add_agentCommissionSet_arr);
                }

                $ajaxdata['code'] = 0;
                $ajaxdata['msg'] = '添加成功';
                $this->ajaxReturn($ajaxdata);
            } else {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = '添加失败，货币已存在';
                $this->ajaxReturn($ajaxdata);
            }
        }
    }
}